#include <iostream>
#include "Record.h"
#include "Schema.h"
#include "DBFile.h"
#include <stdlib.h>
#include "BigQ.h"
#include "ComparisonEngine.h"
#include "File.h"


extern struct AndList *final;

extern "C" {
    int yyparse(void); // defined in y.tab.c
}

int main() {
    Schema schema("catalog", "nation");
    Record record;
    DBFile dbfile;
    int i;
    Page page;
    File file;
    ComparisonEngine ceng;

    
    
    //dbfile.Open("./dbfile_dir/lineitem.bin");
    
    file.Open(1,"./dbfile_dir/lineitem.bin");
    
    file.GetPage(&page,0);
    int count =0;
    while (page.GetFirst(&record))
        count++;
    
    cout << count << endl;
    
    file.GetPage(&page,1);
    count =0;
    while (page.GetFirst(&record))
        count++;
    
    cout << count << endl;
    
            
    
    
    
    /*int whichAtts[MAX_ANDS];
    Type whichTypes[MAX_ANDS];
    int numAttrs = 0;

    dbfile.Open("./dbfile_dir/nation.bin");
    dbfile.MoveFirst();

    OrderMaker CNFordermaker;
    OrderMaker Fileordermaker;

    //whichAtts[0] = 0;
    //whichTypes[0] = Int;
    // whichAtts[1] = 2;
    // whichTypes[1] = Int;
    //numAttrs = 1;

    //CNFordermaker.Initialize(numAttrs, whichAtts, whichTypes);

    whichAtts[0] = 0;
    whichAtts[1] = 1;
    whichTypes[0] = Int;
    whichTypes[1] = String;
    numAttrs = 2;
    Fileordermaker.Initialize(numAttrs, whichAtts, whichTypes);

    cout << "\n enter CNF predicate (when done press ctrl-D):\n\t";
    if (yyparse() != 0) {
        cout << " Error: can't parse your CNF.\n";
        exit(1);
    }

    CNF cnf_pred;
    Record literal;
    cnf_pred.GrowFromParseTree(final, &schema, literal);
    OrderMaker dummy;
    cnf_pred.GetSortOrders(CNFordermaker, dummy);
    
    cout << "-- file order maker --" << endl;
    CNFordermaker.Print();
    cout << "-- ---------------- --" << endl;
    //literal.Print(&schema);

    while (dbfile.GetNext(record)) {
        cout << ceng.Compare(&literal, &CNFordermaker, &record, &Fileordermaker) << endl;

        //cout << ceng.Compare(&literal,&record,&cnf_pred) << endl;

        //cout << ceng.Compare(&literal,&record,&CNFordermaker) << endl;
    }

    dbfile.Close();

    //dbfile.Create("./records.db", heap, NULL);
    //dbfile.Load(schema, "./10M/orders.tbl");
    //dbfile.MoveFirst();
    //dbfile.Open("./nation.db");

    //BigQ bigq;

    
    DBFile myfile;
    //myfile.Open("./SortedPageLenRecords");
    myfile.Open("./heapDB/orders.bin");
    myfile.MoveFirst();
    Record newrecord;

    cout << "========================================================" << endl;
    while (myfile.GetNext(newrecord))
    {
            newrecord.Print(&schema);
    }
    myfile.Close();
     */
    /*dbfile.Close();*/

    /*FILE * file = fopen ( "./10M/lineitem.tbl", "r");
    fclose(file);
    dbfile.Close();
    dbfile.Open("./lineitem.db");
    dbfile.Close();
    File lineitem;
    lineitem.Open(1, "./lineitem.db");
    cout << "total pages in the table : "  << lineitem.GetLength()-1 << endl;
    lineitem.GetPage(&page,0);
    while ( page.GetFirst(&record) )
    {
            record.Print(&schema);
		
    }
	
    while( dbfile.GetNext( record ) )
    {
            record.Print(&schema);
    }
		
    int countPage=0;
    for( i = 0 ; i < 101; i++ ){
                    cout << "fetching page: " << countPage++ << endl;
                    lineitem.GetPage(&page, i);
    }
    fclose(file);	
     */
    return 0;
}
